<?php

namespace App\Exceptions\Custom;


use App\Enums\BaseErrorCode;
use App\Library\FeiShu\SendFs;
use Exception;
use Illuminate\Http\Exceptions\HttpResponseException;
use Symfony\Component\HttpFoundation\Response;

/**
 * @author greenLeaf
 */
class ResponseHttpException extends HttpResponseException
{


    /**
     * @param int $errCode
     * @param Exception|null $exception
     * @param string|null $msg
     * @param array|null $logData
     * @param bool $logError
     * @param bool $fsNotify
     * @param int $httpStatusCode
     * @param string $fsRobot
     * @throws Exception
     */
    public function __construct(
        $errCode,
        Exception $exception = null,
        string $msg = null,
        array $logData = null,
        bool $logError = true,
        bool $fsNotify = true,
        int $httpStatusCode = Response::HTTP_OK,
        string $fsRobot = SendFs::ROBOT_WARNING
    ) {

        $msg = $msg ?? BaseErrorCode::getMessage($errCode);

        if (
            $exception
            &&
            ! $exception instanceof ResponseHttpException
            &&
            ($fsNotify || $logError)
        ) {
            //记录日志｜飞书
            logException("程序运行报错 {$msg}", $exception, $logData, $logError, $fsNotify, $fsRobot);
        }

        parent::__construct(response()->json([
            "code" => $errCode,
            "msg" => $msg,
            "data" => new \stdClass()
        ], $httpStatusCode));
    }
}
